var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var morgan = require('morgan');

var MongoClient = require('mongodb').MongoClient;
var mongoUrl = 'mongodb://localhost:27017/mission';
var _db;

app.use(morgan('dev'));
app.use(bodyParser.json());
app.use(express.static('dist'));

MongoClient.connect(mongoUrl, function (err, db) {
  if(err){
    console.error(err);
    return
  }

  console.log('connected to mongo...');

  _db = db;
  app.listen(8888, function () {
    console.log('server is running!');
  })
})

//  使用post方法
app.post('/create', function (req, res, next) {
  //  接收前端发送的字段
  var mission = req.body;

  //  选择一个my_mission表，如果没有，会自动创建
  var collection = _db.collection('my_mission');

  //如果我们需要的字段不存在，返回前端信息
  if(!mission.comment || !mission.totalTime || !mission.date) {
    res.send({errcode: -1, errmsg: "params missed"});
    return;
  }

  //如果存在就插入数据库，返回OK
  collection.insert({comment: mission.comment, totalTime: mission.totalTime, date: mission.date},function (err, ret) {
    if(err){
      console.error(err);
      res.status(500).end();
    } else {
      res.send({errcode: 0, errmsg: 'OK'});
    }
  });
})
